import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';
import 'package:flutter_jpf_core/util/data_util.dart';

class DialogUtil
{
  static Color _backgroundColor = Color(0xfff1f2f3);
  static Color _okTitleColor = Color(0xff333333);
  static Color _cancelTitleColor = Color(0xff333333);
  static double _maxListViewHeight = 400;

  static config({Color backgroundColor,Color okTitleColor,Color cancelTitleColor,double maxListViewHeight=400,}){

    if(backgroundColor != null){
      DialogUtil._backgroundColor = backgroundColor;
    }
    if(okTitleColor != null){
      DialogUtil._okTitleColor = okTitleColor;
    }
    if(cancelTitleColor != null){
      DialogUtil._cancelTitleColor = cancelTitleColor;
    }
    DialogUtil._maxListViewHeight = maxListViewHeight;
  }

  
  // 弹出对话框,标题，内容支持输入组件
  Future<void> showAlertDialog(BuildContext context,String title,String okTitle,String cancelTitle,Function(bool) goOnNext,{String message,Widget body,bool barrierDismissible = true, double height, Color bgColor}) async{
    if(bgColor == null){
      bgColor = _backgroundColor;
    }
    if(height == null){
      if(DataUtil.isStringEmpty(title) || DataUtil.isStringEmpty(message)){
        height = 130;
      }else{
        height = 160;
      }
    }
    bool isSure = await showDialog<bool>(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) {
        //弹出框信息
        List<Widget> childen = [];
        if(body == null){
          childen.add(Expanded(flex: 1,child: Container(child: Text(title,style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold,color: Color(0xff333333)),),padding: EdgeInsets.only(top: 20,left: 10,right: 10,bottom: 10),),));

          if(!DataUtil.isStringEmpty(message)){
            childen.add(Expanded(flex: 1,child:Container(alignment: Alignment.center,child: Text(message,style: TextStyle(fontSize: 14,color: Color(0xff666666)),),)));
          }
        }else{
          if(!DataUtil.isStringEmpty(title)){
            childen.add(Expanded(flex: 1,child: Container(child: Text(title,style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold,color: Color(0xff333333)),),padding: EdgeInsets.only(top: 20,left: 10,right: 10,bottom: 10),),));
            //childen.add(Container(height: 1,color: bgColor,));
          }
          childen.add(Expanded(flex: 1,child:body));
        }
        childen.add(Container(height: 10,));
        childen.add(Container(height: 1,color: bgColor,));
        //添加操作按钮
        Widget actionView = Container(
          height: 45,child: Row(children:
        cancelTitle == null?[
        Expanded(flex: 1,child: GestureDetector(
          child: Text(okTitle,style: TextStyle(fontSize: 14,color: _okTitleColor,) ,textAlign: TextAlign.center,),
          onTap: () {
            //关闭对话框并返回true
            Navigator.of(context).pop(true);
          },
        ),),
            ]:
            [
            Expanded(flex: 1,child:  GestureDetector(
            child: Text(cancelTitle,style: TextStyle(fontSize: 14,color: _cancelTitleColor ,) ,textAlign: TextAlign.center,),
            onTap: () => Navigator.of(context).pop(false), // 关闭对话框
        ),),
        Container(width: 1,color: bgColor,),
        Expanded(flex: 1,child: GestureDetector(
        child: Text(okTitle,style: TextStyle(fontSize: 14,color: _okTitleColor,) ,textAlign: TextAlign.center),
        onTap: () {
        //关闭对话框并返回true
        Navigator.of(context).pop(true);
        },
        ),),
        ],),);
        childen.add(actionView);
        return WillPopScope(
        onWillPop: () async {
          return barrierDismissible;
        },
        child:Dialog(child:
        Container(
        height: height,
        child: Column(children: childen,),)));
      },
    );
    goOnNext(isSure??false);
  }

  Future<int> showListDialog(BuildContext context,List items,Function choose,{Function itemName,double maxHeight=0}) async {
    if(maxHeight<1){
      maxHeight = _maxListViewHeight;
    }
    double height = min(45.0*(items.length).toDouble()+80.0 , maxHeight);
    int index = await showDialog<int>(
      context: context,
      builder: (BuildContext context) {
        var child =
        Container(
          margin: EdgeInsets.all(0),

          height: height,//(items.length < 10 ? 45.0*(items.length).toDouble()+60.0 : ScreenUtil.screenHeight-200.0),
          child: Column(
            children: <Widget>[
              ListTile(title: Text("请选择",textAlign: TextAlign.center,)),
              Expanded(
                  child: ListView.builder(
                    itemCount: items.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        height: 45,
                        child: ListTile(
                          title: Text(itemName != null? itemName(items[index]):items[index],style:  TextStyle(color: ColorsUtil.black_333,fontSize: 14),),
                          onTap: () => Navigator.of(context).pop(index),
                        ),);
                    },
                  )),
            ],
          ),);
        //使用AlertDialog会报错
        return Dialog(child: child);
      },
    );
    if (index != null) {
      choose(items[index]);
    }
    return index;
  }

  static void showPicutre(BuildContext context, Image image, {Function(bool) goOnNext,
    bool barrierDismissible = true,double height=0}) async {
    Size screenSize = MediaQuery.of(context).size;
    Map res = await showDialog<Map>(
      context: context,
      barrierDismissible: barrierDismissible, // 点击外部不消失
      builder: (context) {
        return Dialog(
            elevation: 0,
            backgroundColor: Colors.transparent,
            child:
            Container(
              //color: Colors.orange,
              height: height>0?height:screenSize.height-80,
              margin: EdgeInsets.only(bottom: 60, right: 0, left: 0),
              child:
              Stack(
                  children: [
                    GestureDetector(child:
                    ClipRRect(
                        borderRadius: BorderRadius.circular(10),
                        child: Container(
                          margin: EdgeInsets.only(top: 70),
                          child: image,
                        )),
                      onTap: (){
                        Navigator.pop(context);
                        if(goOnNext != null){
                          goOnNext(true);
                        }
                      },
                    ),
                  ]
              ),
            ));
      },
    );
  }
}